package com.example.yiqingwaichu;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.example.yiqingwaichu.yiqing.FileSavename;
import com.example.yiqingwaichu.yiqing.wkActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;

public class ShowActivity extends AppCompatActivity {
    private Button btn_ok, btn_ok1;
    private TextView fx_time, sp_sfysh, tvwkds;
    private AppCompatEditText et_name, et_xueh, et_fdy, et_xb, et_zy, et_bj, et_cxlx, et_lxfs, et_time,et_fdyshsj;
    private Random random = new Random();


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);



        btn_ok1 = findViewById(R.id.btn_ok1);
        btn_ok = findViewById(R.id.btn_ok);
        et_name = findViewById(R.id.et_name);
        et_xueh = findViewById(R.id.et_xueh);
        et_fdy = findViewById(R.id.et_fdy);
        et_xb = findViewById(R.id.et_xb);
        et_zy = findViewById(R.id.et_zy);
        et_bj = findViewById(R.id.et_bj);
        et_cxlx = findViewById(R.id.et_cxlx);
        et_lxfs = findViewById(R.id.et_lxfs);
        et_time = findViewById(R.id.et_time);
        tvwkds = findViewById(R.id.tvwkds);
        et_fdyshsj=findViewById(R.id.et_fdyshsj);

        getqyt1();
        getqyt();
        tvwkds.setOnClickListener(v -> {
            Intent intent = new Intent(ShowActivity.this, wkActivity.class);
            Toast.makeText(this, "正在打开网课下单平台", Toast.LENGTH_SHORT).show();
            startActivity(intent);

        });



        Map<String, String> userInfo = FileSavename.getUserInfo(this);
        if (userInfo != null) {
            et_name.setText(userInfo.get("name"));
            et_xueh.setText(userInfo.get("xuehao"));
            et_fdy.setText(userInfo.get("fdy"));
            et_xb.setText(userInfo.get("xibu"));
            et_zy.setText(userInfo.get("zhuanye"));
            et_bj.setText(userInfo.get("banji"));
        }


        Intent intent = getIntent();

        et_lxfs.setText("1877550" + random.nextInt(9999));

        String data = intent.getStringExtra("data");
        String url = "http://www.lefx.top/api/member/getMemberData?token=" + data;

        OkGo.<String>get(url).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                try {
                    JSONObject jsonObject = new JSONObject(response.body());
//                    String member_nickname[""] = jsonObject.getString("");
//                    et_name.setText(""+member_nickname);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                ;
            }
        });


        et_time.setOnClickListener(v -> {
            TimePickerDialog timePickerDialog = new TimePickerDialog(ShowActivity.this,
                    new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            et_time.setText("" + hourOfDay + ":" + minute);
                        }
                    }, 20, 33, true);
            timePickerDialog.show();
        });


        et_lxfs.setText("1877550" + random.nextInt(9999));





        DialogActivity dig = new DialogActivity(this);
        dig.show();

        btn_ok.setOnClickListener(v -> {
            passDate();
            Toast.makeText(this, "制作成功啦，请仔细观察是否正确后截图使用", Toast.LENGTH_SHORT).show();
        });

        btn_ok1.setOnClickListener(v -> {
            getOK1();
            Toast.makeText(this, "制作成功啦，请仔细观察是否正确后截图使用", Toast.LENGTH_SHORT).show();
        });

    }


    private void getOK1() {

        Intent intent = new Intent(this, ZtActivity1.class);


        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("xuehao", et_xueh.getText().toString().trim());
        intent.putExtra("fdy", et_fdy.getText().toString().trim());
        intent.putExtra("xb", et_xb.getText().toString().trim());
        intent.putExtra("zy", et_zy.getText().toString().trim());
        intent.putExtra("bj", et_bj.getText().toString().trim());
        intent.putExtra("cxlx", et_cxlx.getText().toString().trim());
        intent.putExtra("lxfs", et_lxfs.getText().toString().trim());
        intent.putExtra("time", et_time.getText().toString().trim());
        intent.putExtra("fdyshsj", et_fdyshsj.getText().toString().trim());
        preservation();


        startActivity(intent);
    }

    private void preservation() {
        String name = et_name.getText().toString().trim();
        String xuehao = et_xueh.getText().toString().trim();
        String fdy = et_fdy.getText().toString().trim();
        String xb = et_xb.getText().toString().trim();
        String zy = et_zy.getText().toString().trim();
        String bj = et_bj.getText().toString().trim();

        boolean isSaveSuccess = FileSavename.saveUserInfo(ShowActivity.this,name,xuehao,xb,zy,bj,fdy);
    }

    private void passDate() {

        Intent intent = new Intent(this, ZtMainActivity.class);


        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("xuehao", et_xueh.getText().toString().trim());
        intent.putExtra("fdy", et_fdy.getText().toString().trim());
        intent.putExtra("xb", et_xb.getText().toString().trim());
        intent.putExtra("zy", et_zy.getText().toString().trim());
        intent.putExtra("bj", et_bj.getText().toString().trim());
        intent.putExtra("cxlx", et_cxlx.getText().toString().trim());
        intent.putExtra("lxfs", et_lxfs.getText().toString().trim());
        intent.putExtra("time", et_time.getText().toString().trim());
        intent.putExtra("fdyshsj", et_fdyshsj.getText().toString().trim());

        preservation();
        startActivity(intent);

    }

    private void getqyt() {

        SimpleDateFormat f = new SimpleDateFormat("  yyyy-MM-                                                                                                                                                 dd ");
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd ");
        Date today = new Date();
        System.out.println("今天是:" + f.format(today));
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);// 今天-1天

        Date today1 = new Date();
        System.out.println("今天是:" + f.format(today1));
        Calendar a = Calendar.getInstance();
        a.setTime(today1);
        a.add(Calendar.DAY_OF_MONTH, -2);// 今天-1天

        Date tomorrow1 = a.getTime();
        Date tomorrow = c.getTime();

        int sj222 = random.nextInt(60);
        int sj23 = random.nextInt(60);

        String sqsj = d.format(tomorrow);

        String[] wcsh = new String[]{d.format(tomorrow), d.format(tomorrow1)};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, wcsh);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


//        tv_sj.setText("申请时间：" + d.format(tomorrow) + " 08:" + sj222);
//        tv_sj1.setText(d.format(tomorrow) + " 08:" + sj222);
//        tv_sj2.setText(d.format(tomorrow)  + " 08:" + (sj222-1));
//        tv_fdysj.setText(f.format(tomorrow) + "  09:"+sj23);

    }

    private void getqyt1() {
        SimpleDateFormat f = new SimpleDateFormat("  yyyy-MM-                                                                                                                                                 dd ");
        SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd ");
        Date today = new Date();
        System.out.println("今天是:" + f.format(today));
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);// 今天+1天

        Date tomorrow = c.getTime();

        int sj222 = random.nextInt(60);
        int sj23 = random.nextInt(60);

        et_fdyshsj.setText(f.format(tomorrow) + "  09:"+sj23);
    }


}
